package com.second.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.second.common.pojo.Role;
import com.second.common.vo.Result;
import com.second.system.service.RoleService;
import com.second.system.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: second-education-parent
 * @description:
 * @author: 王子朋
 * @create: 2023-05-24 20:34
 **/
@RestController
@RequestMapping("system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping("/getByUserId/{userId}")
    public Result getByUserId(@PathVariable String userId) {
        List<Role> all = roleService.findAll();
        List<Role> roles = roleService.findByUserId(userId);
        List<String> roleids = roles.stream().map(item -> item.getId()).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("all", all);
        map.put("roles", roleids);
        return Result.success(map);
    }

    @PostMapping("confirm")
    @ApiOperation("确认分配角色")
    public Result confirm(String userId, String[] roleIds) {
        Integer row = roleService.confirm(userId, roleIds);
        return Result.success();
    }
    @PostMapping("list/{current}/{pageSize}")
    @ApiOperation(value = "分页条件查询角色")
    public Result list(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody RoleVo roleVo){
         Page<Role> page = roleService.list(current,pageSize,roleVo);
         return Result.success(page);
    }

}
